﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NHibernate;
using NHibernate.Criterion;
using ZLCMS.Data.Model;
using ZLCMS.Commands.Data;

namespace ZLCMS.Web.SystemManage.Basics.Shop.ShippingProduct
{
    public partial class SetProductDialog : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(Request["ajax"]))
            {
                    ShopOrderShippingProduct shippingproduct = null;
                using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                {
                    if (!string.IsNullOrEmpty(Request["key"]))
                    {
                        try
                        {
                            shippingproduct = session.Get<ShopOrderShippingProduct>(Int32.Parse(Request["key"]));
                        }
                        catch (System.Exception ex)
                        {

                        }
                    }

                    ShopOrderShippingInfo orderShipping = session.Get<ShopOrderShippingInfo>(Int32.Parse(Request["sid"]));
                    IList<ShopOrderProduct> list = session.CreateCriteria<ShopOrderProduct>().Add(Expression.Eq("OrderNo", orderShipping.OrderNo)).List<ShopOrderProduct>();

                    //设置sku属性
                    foreach (ShopOrderProduct item in list)
                    {
                        string paramCode = session.CreateSQLQuery("select PARAM_CODE from shop_sku_product where PRODUCT_SKU_CODE=:sku")
                            .SetParameter("sku", item.SkuProductNo)
                            .UniqueResult<string>();
                        string str = "";
                        List<SKUGroup> groups = Newtonsoft.Json.JsonConvert.DeserializeObject<List<SKUGroup>>(paramCode);
                        if (groups != null)
                        {
                            foreach (var sku in groups)
                            {
                                string itemName = session.CreateSQLQuery("select ITEM_NAME from shop_sku_item where RECID=" + sku.ItemId)
                                    .UniqueResult<string>();
                                str += "【" + itemName + "】";
                            }
                        }
                        //return str;
                        item.ProductName = item.ProductName + str;
                    }

                    list.Insert(0, new ShopOrderProduct() { SkuProductNo = "-1", ProductName = "--请选择--" });

                    ddl_productlist.DataSource = list;

                }
                ddl_productlist.DataTextField = "ProductName";
                ddl_productlist.DataValueField = "SkuProductNo";
                ddl_productlist.DataBind();

                if (shippingproduct != null)
                {
                    ddl_productlist.SelectedValue = shippingproduct.SkuProductNo;
                    prouctcount.Value = string.Format("{0}", shippingproduct.Quantity);
                }
            }
            else
            {
                using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                {
                    ShopOrderShippingInfo orderShipping = session.Get<ShopOrderShippingInfo>(Int32.Parse(Request["sid"]));
                    ShopOrderShippingProduct shippingProduct = null;

                    if (!string.IsNullOrEmpty(Request["key"]))
                    {
                        try
                        {
                            shippingProduct = session.Get<ShopOrderShippingProduct>(Int32.Parse(Request["key"]));
                        }
                        catch (System.Exception ex)
                        {

                        }
                    }
                    if (shippingProduct == null)
                    {
                        shippingProduct = new ShopOrderShippingProduct();
                        shippingProduct.CommentStatus = "0";
                        shippingProduct.OrderShippingId = orderShipping.Id;
                        shippingProduct.OwnerAccount = orderShipping.OwnerAccount;
                    }
                    shippingProduct.SkuProductNo = Request["skuno"];
                    ShopSkuProduct sku = session.CreateQuery("from ShopSkuProduct where ProductSkuCode=:code")
                        .SetParameter("code", shippingProduct.SkuProductNo)
                        .UniqueResult<ShopSkuProduct>();
                    shippingProduct.ProductNo = sku.ProductNo;
                    shippingProduct.ProductName = sku.ProductSkuTitle;
                    shippingProduct.Quantity = Int32.Parse(Request["count"]);
                    session.SaveOrUpdate(shippingProduct);
                    session.Flush();
                }
            }
        }
    }
}